package com.fancy.projecty.B_流程实例;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class J_指定流程实例的挂起与激活 {


	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;
	/*历史记录的查询*/
	@Autowired
	private HistoryService historyService;


	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;


	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;

	/**
	 * processInstanceId是act_ru_execution表的PROC_INST_ID_字段
	 */
	@Test
	public void c_指定流程实例的挂起与激活() {

		/*3.查询流程实例对象
		 * 这里就不能用BusinessKey进行查询了,不然会出现空指针异常,
		 * 应该使用ProcessInstanceId来进行查询
		 *
		 * processInstanceId是act_ru_execution表的PROC_INST_ID_字段
		 * */
		ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
				.processInstanceId("5001").singleResult();
		//4.得到当前流程定义的实例是否都为暂停状态
		boolean suspended = processInstance.isSuspended();
		String processInstanceId = processInstance.getId();
		//5.判断
		if (suspended) {
			//说明是暂停，就可以激活操作
			runtimeService.activateProcessInstanceById(processInstanceId);
			System.out.println("流程：" + processInstanceId + "激活");
		} else {
			runtimeService.suspendProcessInstanceById(processInstanceId);
			System.out.println("流程定义：" + processInstanceId + "挂起");
		}
	}
	/*激活流程影响的表:
	==>  Preparing: update ACT_RU_TASK SET REV_ = ?, NAME_ = ?, PARENT_TASK_ID_ = ?, PRIORITY_ = ?, CREATE_TIME_ = ?, OWNER_ = ?, ASSIGNEE_ = ?, DELEGATION_ = ?, EXECUTION_ID_ = ?, PROC_DEF_ID_ = ?, DESCRIPTION_ = ?, DUE_DATE_ = ?, CATEGORY_ = ?, SUSPENSION_STATE_ = ?, FORM_KEY_ = ? where ID_= ? and REV_ = ?
	==> Parameters: 5(Integer), 填写请假申请单(String), null, 50(Integer), 2020-01-02 10:13:33.531(Timestamp), null, zhangsan(String), null, 5001(String), holiday:1:2504(String), null, null, null, 1(Integer), null, 5004(String), 4(Integer)
	<==    Updates: 1
	==>  Preparing: update ACT_RU_EXECUTION set REV_ = ?, BUSINESS_KEY_ = ?, PROC_DEF_ID_ = ?, ACT_ID_ = ?, IS_ACTIVE_ = ?, IS_CONCURRENT_ = ?, IS_SCOPE_ = ?, IS_EVENT_SCOPE_ = ?, PARENT_ID_ = ?, SUPER_EXEC_ = ?, SUSPENSION_STATE_ = ?, CACHED_ENT_STATE_ = ?, NAME_ = ? where ID_ = ? and REV_ = ?
	==> Parameters: 5(Integer), 1001(String), holiday:1:2504(String), _3(String), true(Boolean), false(Boolean), true(Boolean), false(Boolean), null, null, 1(Integer), 2(Integer), null, 5001(String), 4(Integer)
	<==    Updates: 1
	*
	* */

}
